function msg_to_addon() {
    var ans = "";
    var server = $("#Server").val();
    var user = $("#User").val();
    var pass = $("#Password").val();

    var conn = $("#autoconn").attr("checked");
    var disconn = $("#autodisconn").attr("checked");

    if(conn == "checked")
        autoconn = 1;
    else
        autoconn = 0;

    if(disconn == "checked")
        autodisconn = 1;
    else
        autodisconn = 0;

    ans = server+"&"+user+"&"+pass+"&"+autoconn+"&"+autodisconn;

    return ans;
}

window.addEventListener('click', function(event) {

    var cur_name = event.target.name.toString();
    var cur_id = event.target.id;

    if(cur_id == "mainpage")
        self.port.emit("send_me_init_msg","");

    if(cur_name == "save_conf") {
        self.port.emit('click', msg_to_addon());
        event.stopPropagation();
        event.preventDefault();
    }

}, false);

self.port.on('init_page', function(message) {

    $("#Server").val(message.server);
    $("#User").val(message.user);
    $("#Password").val(message.pass);

    if(message.autoconn == 1) {
        $("#autoconn").attr("checked","checked");
    }

    if(message.autodisconn == 1) {
        $("#autodisconn").attr("checked","checked");
    }

});
